package com.tianyang.yukuaifu.business.auth.view;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.business.auth.bean.BankRegOutput;
import com.tianyang.yukuaifu.business.auth.bean.BusinessOutput;
import com.tianyang.yukuaifu.business.auth.bean.CompanySettleInput;
import com.tianyang.yukuaifu.business.auth.bean.SampleImageOutput;
import com.tianyang.yukuaifu.business.auth.contract.AuthConfig;
import com.tianyang.yukuaifu.common.cache.CacheManage;
import com.tianyang.yukuaifu.common.cache.CacheModel;
import com.tianyang.yukuaifu.common.interfaces.CommonConfig;
import com.tianyang.yukuaifu.common.utils.DateCheckPopListener;
import com.tianyang.yukuaifu.module.dialog.DateValidDialog;
import com.tianyang.yukuaifu.module.dialog.SampleImageDialog;
import com.tianyang.yukuaifu.module.dialog.interfaces.IDateValidListener;
import com.tianyang.yukuaifu.module.wheel.DateWheel;
import com.tianyang.yukuaifu.module.wheel.interfaces.IWheelDataChangeCallback;
import com.tianyang.yukuaifu.utils.SoftInputUtil;
import com.tianyang.yukuaifu.utils.ToastUtil;
import com.tianyang.yukuaifu.utils.VerifyUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Description：非法人
 * Created by xyj on 2024/10/20
 */
public class AuthUnInCorporateView extends LinearLayout {

    @BindView(R.id.unIdCardFrontImgSdv)
    SimpleDraweeView unIdCardFrontImgSdv;
    @BindView(R.id.unIdCardBackImgSdv)
    SimpleDraweeView unIdCardBackImgSdv;
    @BindView(R.id.unHandImgSdv)
    SimpleDraweeView unHandImgSdv;
    @BindView(R.id.unBankCardImgSdv)
    SimpleDraweeView unBankCardImgSdv;
    @BindView(R.id.proofMchntImgSdv)
    SimpleDraweeView proofMchntImgSdv;
    @BindView(R.id.unIdCardNameEt)
    EditText unIdCardNameEt;
    @BindView(R.id.unIdCardNoEt)
    EditText unIdCardNoEt;
    @BindView(R.id.unIdCardStartDateTv)
    TextView unIdCardStartDateTv;
    @BindView(R.id.unIdCardEndDateTv)
    TextView unIdCardEndDateTv;
    @BindView(R.id.unCardNoEt)
    EditText unCardNoEt;
    @BindView(R.id.unBankNameTv)
    TextView unBankNameTv;
    @BindView(R.id.unBankTelEt)
    EditText unBankTelEt;

    private SampleImageDialog vSampleImageDialog;

    private CompanySettleInput mCompanySettleInput;

    private SampleImageOutput mOutput;

    private DateValidDialog vDateValidDialog;

    private DateCheckPopListener mDateStartPopListener;

    private DateCheckPopListener mDateEndPopListener;

    public AuthUnInCorporateView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View.inflate(getContext(), R.layout.view_auth_un_incorporate, this);
        ButterKnife.bind(this);
        mCompanySettleInput = new CompanySettleInput();
        mOutput = CacheManage.getInstance().getCache(CacheModel.SAMPLE_IMAGE);
    }

    @OnClick({R.id.unIdCardStartDateTv, R.id.unIdCardEndDateTv, R.id.takeUnIdCardFrontTv,
            R.id.takeUnIdCardBackTv, R.id.takeUnHandTv, R.id.takeUnBankCardTv, R.id.takeProofMchntTv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.unIdCardStartDateTv:
                if (mDateStartPopListener == null) {
                    mDateStartPopListener = new DateCheckPopListener((Activity) getContext(), true);
                }
                mDateStartPopListener.handleRegionCheck(view, "请选择有效期开始日期", unIdCardStartDateTv, new IWheelDataChangeCallback() {
                    @Override
                    public void wheelDataChangeCallback(String wheelData) {
                        if (!TextUtils.isEmpty(wheelData)) {
                            mCompanySettleInput.setUnincorporateIdCardStartDate(wheelData);
                        }
                    }
                });
                break;
            case R.id.unIdCardEndDateTv:
                if (vDateValidDialog == null) {
                    vDateValidDialog = new DateValidDialog(getContext());
                }
                vDateValidDialog.setListener(new IDateValidListener() {
                    @Override
                    public void onDateValid(boolean isLongTerm) {
                        if (isLongTerm) {
                            unIdCardEndDateTv.setText("长期");
                            mCompanySettleInput.setUnincorporateIdCardEndDate(CommonConfig.DATE_LONG_TERM);
                        } else {
                            view.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    showEndValidDialog(view);
                                }
                            }, 100);
                        }
                    }
                });
                vDateValidDialog.show();
                break;
            case R.id.takeUnIdCardFrontTv:
                if (mOutput != null) {
                    showSampleImageDialog(mOutput.getShenfengzhengpic());
                }
                break;
            case R.id.takeUnIdCardBackTv:
                if (mOutput != null) {
                    showSampleImageDialog(mOutput.getShenfengfanpic());
                }
                break;
            case R.id.takeUnHandTv:
                if (mOutput != null) {
                    showSampleImageDialog(mOutput.getHoldingCardPic());
                }
                break;
            case R.id.takeUnBankCardTv:
                if (mOutput != null) {
                    showSampleImageDialog(mOutput.getBankcardpic());
                }
                break;
            case R.id.takeProofMchntTv:
                if (mOutput != null) {
                    showSampleImageDialog(mOutput.getProofMchntImgPic());
                }
                break;
        }
    }

    private void showSampleImageDialog(SampleImageOutput.SampleImage sampleImage) {
        if (vSampleImageDialog == null) {
            vSampleImageDialog = new SampleImageDialog(getContext());
        }
        vSampleImageDialog.showPictureDataView(sampleImage);
        vSampleImageDialog.show();
    }

    private void showEndValidDialog(View view) {
        if (mDateEndPopListener == null) {
            mDateEndPopListener = new DateCheckPopListener((Activity) getContext(), true);
        }
        mDateEndPopListener.handleRegionCheck(view, "请选择有效期结束日期", unIdCardEndDateTv, DateWheel.DATE_TYPE_ALL,
                new IWheelDataChangeCallback() {
                    @Override
                    public void wheelDataChangeCallback(final String wheelData) {
                        if (!TextUtils.isEmpty(wheelData)) {
                            mCompanySettleInput.setUnincorporateIdCardEndDate(wheelData);
                        }
                    }
                });
    }

    public void onFeedBackView(CompanySettleInput input) {
        if (input != null) {
            mCompanySettleInput = input;
            if (!TextUtils.isEmpty(mCompanySettleInput.getProofMchntImg()))
                proofMchntImgSdv.setImageURI(Uri.parse(mCompanySettleInput.getProofMchntImg()));
            if (!TextUtils.isEmpty(mCompanySettleInput.getUnincorporateIdCardFrontImage()))
                unIdCardFrontImgSdv.setImageURI(Uri.parse(mCompanySettleInput.getUnincorporateIdCardFrontImage()));
            if (!TextUtils.isEmpty(mCompanySettleInput.getUnincorporateIdCardBackImage()))
                unIdCardBackImgSdv.setImageURI(Uri.parse(mCompanySettleInput.getUnincorporateIdCardBackImage()));
            if (!TextUtils.isEmpty(mCompanySettleInput.getUnincorporateBankCardImage()))
                unBankCardImgSdv.setImageURI(Uri.parse(mCompanySettleInput.getUnincorporateBankCardImage()));
            if (!TextUtils.isEmpty(mCompanySettleInput.getUnincorporateHandheldIdCard()))
                unHandImgSdv.setImageURI(Uri.parse(mCompanySettleInput.getUnincorporateHandheldIdCard()));
            unIdCardNameEt.setText(VerifyUtil.checkString(mCompanySettleInput.getUnincorporateIdCardName()));
            unIdCardNoEt.setText(VerifyUtil.checkString(mCompanySettleInput.getUnincorporateIdCardNo()));
            unIdCardStartDateTv.setText(VerifyUtil.checkString(mCompanySettleInput.getUnincorporateIdCardStartDate()));
            unIdCardEndDateTv.setText(VerifyUtil.checkString(mCompanySettleInput.getUnincorporateIdCardEndDate()));
            unCardNoEt.setText(VerifyUtil.checkString(mCompanySettleInput.getUnincorporateCardNo()));
            unBankNameTv.setText(VerifyUtil.checkString(mCompanySettleInput.getUnincorporateBankName()));
            unBankTelEt.setText(VerifyUtil.checkString(mCompanySettleInput.getUnincorporateBankTel()));
        }
    }

    public void showBankCardInfoView(BankRegOutput output) {
        if (output != null) {
            if (!TextUtils.isEmpty(output.getBank_card_number())) {
                String bankCardId = output.getBank_card_number().replaceAll(" ", "");
                unCardNoEt.setText(bankCardId);
                mCompanySettleInput.setUnincorporateCardNo(bankCardId);
            }
        }
    }

    public void showPhotoView(String urlStr, String photoType) {
        switch (photoType) {
            case CommonConfig.TYPE_UNIDFROPIC:
                unIdCardFrontImgSdv.setImageURI(Uri.parse(urlStr));
                mCompanySettleInput.setUnincorporateIdCardFrontImage(urlStr);
                break;
            case CommonConfig.TYPE_UNIDCONPIC:
                unIdCardBackImgSdv.setImageURI(Uri.parse(urlStr));
                mCompanySettleInput.setUnincorporateIdCardBackImage(urlStr);
                break;
            case CommonConfig.TYPE_UNHANDPIC:
                unHandImgSdv.setImageURI(Uri.parse(urlStr));
                mCompanySettleInput.setUnincorporateHandheldIdCard(urlStr);
                break;
            case CommonConfig.TYPE_UNBANKCARDPIC:
                unBankCardImgSdv.setImageURI(Uri.parse(urlStr));
                mCompanySettleInput.setUnincorporateBankCardImage(urlStr);
                break;
            case CommonConfig.TYPE_PROOFMCHNTPIC:
                proofMchntImgSdv.setImageURI(Uri.parse(urlStr));
                mCompanySettleInput.setProofMchntImg(urlStr);
                break;
        }
    }

    public void showBusinessView(BusinessOutput output) {
        if (output != null) {
            unBankNameTv.setText(VerifyUtil.checkString(output.getBranchBankName()));
            mCompanySettleInput.setUnincorporateBankName(output.getBranchBankName());
            mCompanySettleInput.setUnincorporateBankNo(output.getBranchNo());
        }
    }

    public void setOnlyReadView() {
        unIdCardNameEt.setEnabled(false);
        unIdCardNoEt.setEnabled(false);
        unIdCardStartDateTv.setEnabled(false);
        unIdCardStartDateTv.setCompoundDrawables(null, null, null, null);
        unIdCardEndDateTv.setEnabled(false);
        unIdCardEndDateTv.setCompoundDrawables(null, null, null, null);
        unCardNoEt.setEnabled(false);
        unBankNameTv.setEnabled(false);
        unBankNameTv.setCompoundDrawables(null, null, null, null);
        unBankTelEt.setEnabled(false);
    }

    public CompanySettleInput getSettleInput() {
        mCompanySettleInput.setUnincorporateIdCardName(unIdCardNameEt.getText().toString());
        mCompanySettleInput.setUnincorporateIdCardNo(unIdCardNoEt.getText().toString());
        mCompanySettleInput.setUnincorporateCardNo(unCardNoEt.getText().toString());
        mCompanySettleInput.setUnincorporateBankTel(unBankTelEt.getText().toString());
        mCompanySettleInput.setSettleWay(AuthConfig.SETTLE_WAY_FFR);
        return mCompanySettleInput;
    }

    public boolean checkSettleInfo() {
        if (TextUtils.isEmpty(mCompanySettleInput.getUnincorporateIdCardFrontImage())) {
            ToastUtil.show("请上传非法人身份证人像面");
            return false;
        }
        if (TextUtils.isEmpty(mCompanySettleInput.getUnincorporateIdCardBackImage())) {
            ToastUtil.show("请上传非法人身份证国徽面");
            return false;
        }
        if (TextUtils.isEmpty(mCompanySettleInput.getUnincorporateHandheldIdCard())) {
            ToastUtil.show("请上传非法人手持身份证照");
            return false;
        }
        if (TextUtils.isEmpty(mCompanySettleInput.getUnincorporateBankCardImage())) {
            ToastUtil.show("请上传非法人银行卡卡号照");
            return false;
        }
        if (TextUtils.isEmpty(mCompanySettleInput.getProofMchntImg())) {
            ToastUtil.show("请上传非法人结算证明");
            return false;
        }
        if (TextUtils.isEmpty(unIdCardNameEt.getText())) {
            ToastUtil.show("请输入非法人姓名");
            SoftInputUtil.openKeybord(unIdCardNameEt, getContext());
            return false;
        }
        if (TextUtils.isEmpty(unIdCardNoEt.getText())) {
            ToastUtil.show("请输入非法人证件号码");
            SoftInputUtil.openKeybord(unIdCardNoEt, getContext());
            return false;
        }
        if (TextUtils.isEmpty(unIdCardStartDateTv.getText())) {
            ToastUtil.show("请选择非法人证件有效期开始日期");
            return false;
        }
        if (TextUtils.isEmpty(unIdCardEndDateTv.getText())) {
            ToastUtil.show("请选择非法人证件有效期结束日期");
            return false;
        }
        if (TextUtils.isEmpty(unCardNoEt.getText())) {
            ToastUtil.show("请填写非法人银行卡号");
            SoftInputUtil.openKeybord(unCardNoEt, getContext());
            return false;
        }
        if (TextUtils.isEmpty(unBankNameTv.getText())) {
            ToastUtil.show("请选择非法人开户支行");
            return false;
        }
        if (TextUtils.isEmpty(unBankTelEt.getText())) {
            ToastUtil.show("请填写非法人预留手机号");
            SoftInputUtil.openKeybord(unBankTelEt, getContext());
            return false;
        } else if (VerifyUtil.checkPhoneNumber(unBankTelEt.getText())) {
            ToastUtil.show("请输入非法人正确的手机号");
            SoftInputUtil.openKeybord(unBankTelEt, getContext());
            return false;
        }
        return true;
    }

    public void onDestroyView() {
        if (vSampleImageDialog != null && vSampleImageDialog.isShowing()) {
            vSampleImageDialog.dismiss();
            vSampleImageDialog = null;
        }
        if (vDateValidDialog != null) {
            vDateValidDialog.dismiss();
            vDateValidDialog = null;
        }
    }
}
